package com.hanrui.android.fragment.message.audit.probusinfo;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.blankj.utilcode.util.ToastUtils;
import com.hanrui.android.R;
import com.hanrui.android.activity.TermActivity;
import com.hanrui.android.library.base.BaseFragment;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.model.GetNextActvityBean;
import com.hanrui.android.model.ImpSubBean;
import com.hanrui.android.model.SubEntryBean;
import com.hanrui.android.net.contract.message.audit.probusinfo.ProBusInfoBizContract;
import com.hanrui.android.net.presenter.message.audit.probusinfo.ProBusInfoBizPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 任务书审批流程
 */

public class ProBusInfoBizFragment extends BaseFragment<ProBusInfoBizPresenter> implements ProBusInfoBizContract.View {

    @BindView(R.id.next)
    LinearLayout nextLl;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.name_go)
    ImageView nameGo;
    @BindView(R.id.bm_ll)
    LinearLayout bmLl;
    @BindView(R.id.bm)
    TextView bm;
    @BindView(R.id.bm_go)
    ImageView bmGo;
    @BindView(R.id.reson_ll)
    LinearLayout resonLl;
    @BindView(R.id.reson)
    EditText reson;
    @BindView(R.id.prosubject_ll)
    LinearLayout prosubjectLl;
    @BindView(R.id.prosubject)
    TextView prosubject;
    @BindView(R.id.prosubject_go)
    ImageView prosubjectGo;
    @BindView(R.id.impsubject_ll)
    LinearLayout impsubjectLl;
    @BindView(R.id.impsubject)
    TextView impsubject;
    @BindView(R.id.impsubject_go)
    ImageView impsubjectGo;

    String taskId,procInsId,bizId;
    String transitionId = "";
    String nextAssigneeList = "";
    String subEntry = "";
    String impSub = "";

    GetNextActvityBean.NextActivityBean bean;

    OptionsPickerView pvOptions,subOptions,impOptions;

    ArrayList<GetNextActvityBean.NextActivityBean.NextAssigneeListBean> dataList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_probusinfobiz;
    }

    @Override
    protected void initInjector() { mFragmentComponent.inject(this); }

    @Override
    protected void initView(View view) {

        taskId = getArguments().getString("taskId");
        procInsId = getArguments().getString("procInsId");
        bizId = getArguments().getString("bizId");

        mPresenter.getNextActivity(getActivity(),taskId,procInsId,bizId);

    }

    //实例化
    public static ProBusInfoBizFragment newInstance(String taskId, String procInsId, String bizId) {
        ProBusInfoBizFragment fragment = new ProBusInfoBizFragment();
        Bundle bundle = new Bundle();
        bundle.putString("taskId", taskId);
        bundle.putString("procInsId", procInsId);
        bundle.putString("bizId", bizId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void getNextActivity(GetNextActvityBean data) {

        name.setText(data.getNextActivity().getName());
        bean = data.getNextActivity();

        if (bean.getType().equals("endEvent")) {

            bmLl.setVisibility(View.GONE);
            resonLl.setVisibility(View.GONE);
            reson.setText("");
            return;

        		}


        if (data.getNextActivity().getPrvName().equals("安监部部长")) {

            mPresenter.getSubEntry(getActivity());
            mPresenter.getImpSub(getActivity());
            prosubjectLl.setVisibility(View.VISIBLE);
            impsubjectLl.setVisibility(View.VISIBLE);

        }else {
            prosubjectLl.setVisibility(View.GONE);
            impsubjectLl.setVisibility(View.GONE);
        }

        if (null != bean.getTransitions() && bean.getTransitions().size() > 0) {

            List<GetNextActvityBean.NextActivityBean.TransitionsListBean> transitionsList = bean.getTransitions();

            name.setText(transitionsList.get(0).getName());
            transitionId = transitionsList.get(0).getId();

            if (null != transitionsList.get(0).getNextAssigneeList() && transitionsList.get(0).getNextAssigneeList().size() >0) {

                bm.setText(transitionsList.get(0).getNextAssigneeList().get(0).getName());
                nextAssigneeList = transitionsList.get(0).getNextAssigneeList().get(0).getId();

                getTransitionNextActivity(transitionsList.get(0).getNextAssigneeList());

            }else {

                bmLl.setVisibility(View.GONE);
                nextAssigneeList = "";

            }

            if (transitionsList.size() == 1) {

                name.setText(bean.getName());
                nextLl.setEnabled(false);
                nameGo.setVisibility(View.GONE);


            		}else {

                nextLl.setEnabled(true);
                nameGo.setVisibility(View.VISIBLE);

                pvOptions = new OptionsPickerView.Builder(getActivity(), new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        transitionId = bean.getTransitions().get(options1).getId();
                        name.setText(bean.getTransitions().get(options1).getPickerViewText());
                        if (null != bean.getTransitions().get(options1).getNextAssigneeList() &&
                                bean.getTransitions().get(options1).getNextAssigneeList().size() > 0) {

                            bmLl.setVisibility(View.VISIBLE);
                            bm.setText(bean.getTransitions().get(options1).getNextAssigneeList().get(0).getName());
                            nextAssigneeList = bean.getTransitions().get(options1).getNextAssigneeList().get(0).getId();
                            getTransitionNextActivity(bean.getTransitions().get(options1).getNextAssigneeList());

                        }else {

                            bmLl.setVisibility(View.GONE);
                            bm.setText("");
                            bm.setHint("请选择");
                            nextAssigneeList = "";
                        }

                    }
                }).build();
                pvOptions.setPicker(bean.getTransitions());

            }

        		}else {


            if (null != bean.getNextAssigneeList() && bean.getNextAssigneeList().size() > 0) {

                nextAssigneeList = bean.getNextAssigneeList().get(0).getId();
                bm.setText(bean.getNextAssigneeList().get(0).getName());
                getTransitionNextActivity(bean.getNextAssigneeList());

            }else {

                bmLl.setVisibility(View.GONE);
                nextAssigneeList = "";
            }



        }

    }


    private void getTransitionNextActivity(List<GetNextActvityBean.NextActivityBean.NextAssigneeListBean> nextAssigneeList) {

        if (nextAssigneeList.size() == 1) {

            bmLl.setEnabled(false);
            bmGo.setVisibility(View.GONE);

        }else {

            bmLl.setEnabled(true);
            bmGo.setVisibility(View.VISIBLE);
            dataList.clear();
            dataList.addAll(nextAssigneeList);
            bmLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    TermActivity.start(ProBusInfoBizFragment.this, Constant.nextactivity,dataList,false);
                }
            });

        }

    }

    @Override
    public void proBusInfoBizApproval(int code) {
        if (code == 1) {

            ToastUtils.showShort("提交成功");
            getActivity().finish();

        		}else {

            ToastUtils.showShort("提交失败");
        }

    }

    @Override
    public void getSubEntry(final SubEntryBean data) {
        if (null == data || null == data.getSubEntry() || data.getSubEntry().size() == 0) {
            return;
        }

        subOptions = new OptionsPickerView.Builder(getActivity(), new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {

                prosubject.setText(data.getSubEntry().get(options1).getName());
                subEntry = data.getSubEntry().get(options1).getId();

            }
        }).build();

        subOptions.setPicker(data.getSubEntry());


        prosubjectLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                subOptions.show();

            }
        });


    }

    @Override
    public void getImpSub(final ImpSubBean data) {

        if (null == data || null == data.getImpSub() || data.getImpSub().size() == 0) {
            return;
        }

        impOptions = new OptionsPickerView.Builder(getActivity(), new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {

                impsubject.setText(data.getImpSub().get(options1).getName());
                impSub = data.getImpSub().get(options1).getId();

            }
        }).build();

        impOptions.setPicker(data.getImpSub());


        impsubjectLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                impOptions.show();

            }
        });
    }


    @OnClick({R.id.next_ll,R.id.login_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.next_ll:
                if (null != pvOptions)
                    pvOptions.show();
                break;
            case R.id.login_out:
                if (bean.getPrvName().equals("安监部部长")) {
                    if (TextUtils.isEmpty(subEntry)) {

                        ToastUtils.showShort("请选择项目主体");
                        return;

                    		}
                    if (TextUtils.isEmpty(impSub)) {
                        ToastUtils.showShort("请选择实施主体");
                        return;
                    		}

                		}
                mPresenter.proBusInfoBizApproval(getActivity(),nextAssigneeList,taskId,procInsId,bizId,transitionId,reson.getText().toString(),subEntry,impSub);
                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case Constant.nextactivity:
                if (null != data) {
                    String result = data.getStringExtra("data");
                    String countING = data.getStringExtra("id");
                    if (null != result && !"".equals(result)) {
                        bm.setText(result);
                    }
                    nextAssigneeList = countING;
                }
                break;
        }
    }
}
